package main

import (
	"github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/mvc"
	"fmt"
	"encoding/json"
)

type ProductType struct{
	Id int
	Name string 
}

type Product struct{
	Id int 
	Name string 
	Type ProductType 
	TypeId int
}

type ProjectType struct{
	Id int 
	Name string 
}

type Project struct{
	Id int 
	Name string
	Type ProjectType 
	TypeId int
}

type Meal struct{
	Id int 
	Name string 
	ProjectList []*Project 
	ProductList []*Product 
}

type Member struct{
	Id int 
	Name string 
	Meallist []*Meal 
	Hobbys map[string]Hobby
}

type Hobby struct{
	Id int 
	Name string
	Fields map[string]Field 
}

type Field struct{
	Id int 
	Name string 
}

type JsonController struct{
	Ctx iris.Context
}

func (c *JsonController) BeforeActivation(b mvc.BeforeActivation) {

    b.Handle(
        "POST",
        "/custom",
        "JsonHandler",
    )

}

func (j *JsonController) JsonHandler() interface{} {
	var mb []*Member

	name := j.Ctx.FormValue("name")
    age := j.Ctx.FormValue("age")
	if err := json.Unmarshal([]byte(age), &mb); err == nil{
    //if err := j.Ctx.ReadJSON(&mb); err == nil{
		for _, jv := range mb{
			fmt.Println(jv)
			for k, v := range jv.Hobbys{
				for fk, fv := range v.Fields{
					fmt.Println(fk,">>>", fv.Id, fv.Name)
				}
				fmt.Println(k,">>>", v.Id, v.Name)
			}
			for _, mv := range jv.Meallist{
				fmt.Println(mv)
				for _, mvj := range mv.ProjectList{
					fmt.Println(mvj)
				}
				for _, mvd := range mv.ProductList{
					fmt.Println(mvd)
				}
			}
		}

		return map[string]interface{}{"message": "成功","code":200, "信息":name}

	}else{
		return map[string]interface{}{"message": "error", "code":400}
	}
    
}

func main() {
	app := iris.New()
	
    mvc.Configure(app.Party("/web"), func(context *mvc.Application){
        context.Handle(new(JsonController))
	})
	
    app.Run(iris.Addr(":2222"))
}